package com.springboot.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.springboot.demo.common.customValidator.ListNotHasNull;
import com.springboot.demo.entity.*;
import com.springboot.demo.exception.BusinessException;
import com.springboot.demo.service.InboxService;
import com.springboot.demo.utils.ResponseResult;
import com.springboot.demo.utils.ResponseUtils;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-09-01 9:37
 */
@RestController
@Api(tags = "收件箱接口")
@Validated
public class InboxController {
    @Autowired
    InboxService inboxService;
    @Autowired
    ResponseUtils responseUtils;

    @GetMapping("/inboxes")
    @ApiOperation(value = "根据条件搜索消息")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "state", value = "消息状态,0:未读，1：已读"),
            @ApiImplicitParam(name = "startDate", value = "开始日期"),
            @ApiImplicitParam(name = "endDate", value = "结束日期"),
            @ApiImplicitParam(name = "search", value = "搜索(内容）,1~32位"),
            @ApiImplicitParam(name = "sort",
                    value = "排序,格式：列字段+空格+asc或者desc，例如：\"createDate desc\" ,可排序字段：[readed，createDate]"),
            @ApiImplicitParam(name = "type", value = "消息类型,0:报警，1:事件，2:工单，3:报修，4：维保，5：系统")
    })
    public ResponseUtils<PageResult<List<Inbox>>> getMsg(@RequestParam(value = "state", required = false) Integer state,
                                                         @RequestParam(value = "startDate", required = false)
                                                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                                         @RequestParam(value = "endDate", required = false)
                                                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
                                                         @Length(max = 32, message = "搜索输入长度超过限制")
                                                         @ApiParam(value = "1~32位",required = false)
                                                         @RequestParam(value = "search", required = false) String search,
                                                         @RequestParam(value = "sort", required = false) String sort,
                                                         @RequestParam(value = "type", required = false) Integer type,
                                                         @Valid PageRequest pageQuery) {
        // 获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        Long uid = userDetailsImpl.getUser().getId();
        PageResult msg = inboxService.getMsg(uid, state, type, startDate, endDate, search,sort, pageQuery);
        return responseUtils.buildSuccessResponse(msg);
    }

    @DeleteMapping("/inboxes")
    @ApiOperation("清空所有消息")
    @ApiOperationSupport(order = 20)
    public ResponseUtils deleteAllMsg() {
        // 获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        Long uid = userDetailsImpl.getUser().getId();
        inboxService.deleteAllMsg(uid);
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/inbox")
    @ApiOperation(value = "删除勾选消息", notes = "msgIds传消息id数组，[1,2,3...]")
    @ApiOperationSupport(order = 30)
    public ResponseUtils deleteMsg(@RequestBody @Size(min = 1) @ListNotHasNull List<Long> msgIds) {
        // 获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        Long uid = userDetailsImpl.getUser().getId();
        inboxService.deleteMsg(uid, msgIds);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/inboxes/readed")
    @ApiOperation("标记所有消息为已读")
    @ApiOperationSupport(order = 40)
    public ResponseUtils markAllMsgReaded() {
        // 获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        Long uid = userDetailsImpl.getUser().getId();
        inboxService.markAllMsgReaded(uid);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/inbox/readed")
    @ApiOperation(value = "标记勾选消息为已读", notes = "msgIds传消息id数组,[1,2,3...]")
    @ApiOperationSupport(order = 50)
    public ResponseUtils markMsgReaded(@RequestBody @ListNotHasNull @Size(min = 1) List<Long> msgIds) {
        // 获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        Long uid = userDetailsImpl.getUser().getId();
        inboxService.markMsgReaded(uid, msgIds);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/inbox/{msgId}")
    @ApiOperation(value = "获取消息内容")
    @ApiOperationSupport(order = 60)
    public ResponseUtils<Inbox> getMsgInfo(@PathVariable("msgId") @ApiParam(value = "消息id", required = true) Long msgId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        Long uid = userDetailsImpl.getUser().getId();
        Inbox msgInfo = inboxService.getMsgInfo(uid, msgId);
        return responseUtils.buildSuccessResponse(msgInfo);
    }
}
